package com.kkshop.gwt.registration;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.kkshop.gwt.service.UserUtilsService;
import com.kkshop.gwt.service.UserUtilsServiceAsync;
import com.kkshop.gwt.widget.wizard.impl.AbstractWizardPage;

public class AccountInformationWizardPage extends AbstractWizardPage {
    
    private Label usernameLabel;
    private TextBox usernameText;
    private Label passwordLabel;
    private PasswordTextBox passwordText;
    private boolean usernameOk;
    private boolean busy;
    
    public AccountInformationWizardPage() {
        super(RegistrationController.getInstance().getResources().registerAccountInformationHeader());
    }

    public Widget createWidget() {
        FlexTable table = new FlexTable();
        table.setWidget(0, 0, getUsernameLabel());
        table.setWidget(0, 1, getUsernameText());
        table.setWidget(1, 0, getPasswordLabel());
        table.setWidget(1, 1, getPasswordText());
        
        getUsernameText().addChangeListener(new ChangeListener() {
            public void onChange(Widget widget) {
                busy = true;
                
                ((RegistrationWizard) getWizard()).getModel().setUsername(getUsernameText().getText());
                
                UserUtilsServiceAsync userUtils = UserUtilsService.UserUtilsServiceHelper.getInstance().getService();
                userUtils.userExists(getUsernameText().getText(), new AsyncCallback() {
                    public void onFailure(Throwable t) {
                        usernameOk = false;
                        setErrorMessage(t.getMessage());
                        busy = false;
                        doUpdateButtons();
                    }

                    public void onSuccess(Object result) {
                        usernameOk = Boolean.FALSE.equals(result);
                        if(!usernameOk) {
                            setErrorMessage("User name already exits");
                        }
                        busy = false;
                        doUpdateButtons();
                    }
                });
            }
        });
        getPasswordText().addChangeListener(new ChangeListener() {
            public void onChange(Widget widget) {
                ((RegistrationWizard) getWizard()).getModel().setPassword(getPasswordText().getText());
            }
        });
        
        return table;
    }

    public boolean isFinished() {
        if("".equals(getUsernameText().getText())) {
            setErrorMessage("User name must not be empty");
            return false;
        } else if(busy || !usernameOk) {
            return false;
        } else {
            setErrorMessage("");
            return true;
        }
    }

    private Label getUsernameLabel() {
        if(usernameLabel == null) {
            usernameLabel = new Label("User name:");
        }
        return usernameLabel;
    }

    private TextBox getUsernameText() {
        if(usernameText == null) {
            usernameText = new TextBox();
        }
        return usernameText;
    }

    private Label getPasswordLabel() {
        if(passwordLabel == null) {
            passwordLabel = new Label("Password:");
        }
        return passwordLabel;
    }

    private PasswordTextBox getPasswordText() {
        if(passwordText == null) {
            passwordText = new PasswordTextBox();
        }
        return passwordText;
    }
}
